package com.xwj.back.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xwj.back.mapper.NoticeMapper;
import com.xwj.back.pojo.Notice;
import com.xwj.back.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public JSONObject getNotices(Integer page) {
        IPage<Notice> noticePage = new Page<>(page,9);
        JSONObject resp = new JSONObject();
        Long total = noticeMapper.selectCount(null);
        List<Notice> notices = noticeMapper.selectPage(noticePage, null).getRecords();
        resp.put("total",total);
        resp.put("notices",notices);
        return resp;
    }

    @Override
    public List<Notice> getSendAll() {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("send", true);
        return noticeMapper.selectList(queryWrapper);
    }

    @Override
    public String addNotice(Notice notice) {
        Date date = new Date();
        notice.setCreateTime(date);
        if(notice.isSend()){
            notice.setSendTime(date);
        }
        noticeMapper.insert(notice);
        return "success";
    }

    @Override
    public String deleteNotice(Integer id) {
        noticeMapper.deleteById(id);
        return "success";
    }

    @Override
    public String updateNotice(Map<String, String> data) {
        Integer id = Integer.valueOf(data.get("id"));
        String title = data.get("title");
        String content = data.get("content");
        String type = data.get("type");
        boolean send = Boolean.parseBoolean(data.get("send"));
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id)
                .set("title",title)
                .set("content",content)
                .set("type",type)
                .set("send",send);
        noticeMapper.update(null,updateWrapper);
        return "success";
    }

    @Override
    public String sendNotice(Integer id) {
        Date date = new Date();
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("send",true).set("send_time",date);
        noticeMapper.update(null,updateWrapper);
        return "success";
    }

    @Override
    public List<Notice> getLike(String type,boolean send) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type).eq("send",send);
        return noticeMapper.selectList(queryWrapper);
    }

    @Override
    public String bulkDelete(List<Notice> notices) {
        for (Notice notice : notices) {
            noticeMapper.deleteById(notice);
        }
        return "success";
    }
}
